import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ResponseInterceptor } from './common/interceptor/response';
import { ValidationPipe } from '@nestjs/common';
// import { GlobalGuard } from './common/guards/global.guard';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
import rateLimit from 'express-rate-limit';
import { HttpExceptionsFilter } from './common/filters/http-exceptions-filter';
async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    cors: true, // 开启跨域访问
  });
  const config = app.get(ConfigService);
  // 设置api访问前缀
  const prefix: string = config.get<string>('app.prefix') as string;
  app.setGlobalPrefix(prefix);
  // 设置访问频率
  app.use(
    rateLimit({
      windowMs: 15 * 60 * 1000, // 15分钟
      max: 1000, // 限制15分钟内最多只能访问1000次
    }),
  );
  // swagger 接口文档
  const swaggerOptions = new DocumentBuilder().setTitle('皓石-物流管理系统').setDescription('皓石-物流管理系统接口文档').setVersion('0.0.1').build();
  const swaggerDocument = SwaggerModule.createDocument(app, swaggerOptions);
  SwaggerModule.setup(`${prefix}/api-docs`, app, swaggerDocument);
  // 全局响应拦截
  app.useGlobalInterceptors(new ResponseInterceptor());
  app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));
  // 全局异常过滤
  app.useGlobalFilters(new HttpExceptionsFilter());
  // app.useGlobalGuards(new GlobalGuard());
  const port = (config.get<number>('app.port') as number) || 8080;
  await app.listen(port);
  console.log(`皓石-物流管理系统 服务启动成功\n` + '服务地址', `http://localhost:${port}${prefix}/`, '\n');
}
bootstrap();
